<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                           |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Other;

/**
 * 渠道来源管理
 *
 * @author Xmh
 */
class Channel {

    private $ComeFrom;
    private $from_id = '';

    public function __construct($from_id) {
        $this->ComeFrom = M("come_from");
        $this->from_id = $from_id;
    }

    /**
     * 获得渠道来源列表
     */
    public function get_channel_list($cols = '*') {
        // $where['from_id'] = array('neq',5);
        // $where['pid'] = array('neq',5);
        return $this->ComeFrom->field($cols)->where($where)->select();
    }
    /**
     * 获得子渠道
     */
    public function get_children_channel() {
        $map['pid'] = $this->from_id;
        return $this->ComeFrom->where($map)->select();
    }
    /**
     * 获取子渠道的from_id集合
     */
    public function get_children_channel_from_id_list() {
        $map['pid'] = $this->from_id;
        $list = $this->ComeFrom->where($map)->select();
        foreach($list as $k=>$v){
            $from_id_list[$k] = $v['from_id'];
        }
        return $from_id_list;
    }
    /**
     * 获取渠道来源详情
     */
    public function get_channel($from_id=0){
        if($from_id<1){
            $from_id=$this->from_id;
        }
        return $this->ComeFrom->find($from_id);
    }
    /**
     * 根据父渠道名称获取渠道详情
     * @param type $parent_name
     * @return type
     */
    public function getChannelDetailByParentName($parent_name) {
        $map['from_name'] = $parent_name;
        $map['pid'] = array('eq','-2');
        return $this->ComeFrom->where($map)->find();
    }
    /**
     * 根据渠道名称获取渠道详情
     * @param type $from_name
     * @return type
     */
    public function getChannelDetailByName($from_name) {
        $map['from_name'] = $from_name;
        $map['pid'] = array('neq','-2');
        return $this->ComeFrom->where($map)->find();
    }
    /**
     * 根据ID获取渠道详情
     * @param type $from_name
     * @return type
     */
    public function getChannelByFromId($pid) {
        $map['from_id'] = $pid;
        return $this->ComeFrom->where($map)->find();
    }

    /**
     * 添加渠道来源
     */
    public function add_channel($data) {
        $data['createtime'] = date("Y-m-d H:i:s");
        return $this->ComeFrom->add($data);
    }

    /**
     * 编辑渠道来源
     */
    public function edit_channel($data) {
        return $this->ComeFrom->save($data);
    }

    /**
     * 删除渠道来源
     */
    public function del_channel() {
        $map['from_id'] = $this->from_id;
        return $this->ComeFrom->where($map)->delete();
    }

}
